package com.jojo.android.zxlib.tool;

import android.graphics.Color;
import android.graphics.Typeface;
import androidx.annotation.ColorInt;
import androidx.annotation.DrawableRes;
import android.text.TextUtils;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;


import com.jojo.android.zxlib.R;


/**
 * Created by fenghao on 2016/11/9.
 * 功能描述：
 */

public class ZxToast {

    /**
     * 显示成功图标
     */
    public static final int ICON_TYPE_SUCCESS = 2;
    /**
     * 显示失败图标
     */
    public static final int ICON_TYPE_FAIL = 3;
    /**
     * 显示信息图标
     */
    public static final int ICON_TYPE_INFO = 4;


    public static void showIconCustomToast(int gravity, int yOffset, int iconType, String msg) {
        if (TextUtils.isEmpty(msg)) return;
        LinearLayout view = new LinearLayout(ZxTool.getContext());
        view.setOrientation(LinearLayout.VERTICAL);
        view.setGravity(Gravity.CENTER);
        ImageView imageView = new ImageView(ZxTool.getContext());
        switch (iconType) {
            case ICON_TYPE_SUCCESS:
                imageView.setImageResource(R.drawable.zx_icon_notify_done);
                break;
            case ICON_TYPE_FAIL:
                imageView.setImageResource(R.drawable.zx_icon_notify_error);
                break;
            case ICON_TYPE_INFO:
                imageView.setImageResource(R.drawable.zx_icon_notify_info);
                break;
        }
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(-1, -1);
        params.bottomMargin = ZxTool.dip2px(5);
        view.addView(imageView, params);
        TextView msgView = new TextView(ZxTool.getContext());
        msgView.setTextSize(14);
        msgView.setText(msg);
        msgView.setGravity(Gravity.CENTER);
        msgView.setTextColor(Color.parseColor("#ffffff"));
        msgView.setTypeface(Typeface.DEFAULT);
        view.addView(msgView);
        view.setBackgroundResource(R.drawable.bg_black_alpha);
        int d = ZxTool.dp2px(1);
        view.setPadding(30 * d, 20 * d, 30 * d, 20 * d);
        Toast toast = new Toast(ZxTool.getContext());
        toast.setView(view);
        toast.setGravity(gravity, 0, yOffset);
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.show();
    }

    public static void showSuccessCenterToast(String msg) {
        showIconCustomToast(Gravity.CENTER, 0, ICON_TYPE_SUCCESS, msg);
    }

    public static void showFAILCenterToast(String msg) {
        showIconCustomToast(Gravity.CENTER, 0, ICON_TYPE_FAIL, msg);
    }

    public static void showINFOCenterToast(String msg) {
        showIconCustomToast(Gravity.CENTER, 0, ICON_TYPE_INFO, msg);
    }

    public static void showSuccessBottomToast(String msg) {
        showIconCustomToast(Gravity.BOTTOM, ZxTool.dp2px(100), ICON_TYPE_SUCCESS, msg);
    }

    public static void showFAILBottomToast(String msg) {
        showIconCustomToast(Gravity.BOTTOM, ZxTool.dp2px(100), ICON_TYPE_FAIL, msg);
    }

    public static void showINFOBottomToast(String msg) {
        showIconCustomToast(Gravity.BOTTOM, ZxTool.dp2px(100), ICON_TYPE_INFO, msg);
    }


    private static void showCustomizeToast(int gravity, int yOffset, @ColorInt int textColor, @DrawableRes int bg, String msg) {
        if (TextUtils.isEmpty(msg)) return;
        LinearLayout view = new LinearLayout(ZxTool.getContext());
        view.setOrientation(LinearLayout.VERTICAL);
        TextView msgView = new TextView(ZxTool.getContext());
        msgView.setTextSize(14);
        msgView.setText(msg);
        msgView.setTextColor(textColor);
        msgView.setTypeface(Typeface.DEFAULT);
        view.addView(msgView);
        view.setBackgroundResource(bg);
        int d = ZxTool.dp2px(1);
        view.setPadding(20 * d, 10 * d, 20 * d, 10 * d);
        Toast toast = new Toast(ZxTool.getContext());
        toast.setView(view);
        toast.setGravity(gravity, 0, yOffset);
        toast.show();
    }


    /**
     * 黑色
     *
     * @param msg
     */
    public static void showCenterToastLight(String msg) {
        showCustomizeToast(Gravity.CENTER, 0, Color.parseColor("#ffffff"), R.drawable.bg_black_alpha, msg);
    }


    /**
     * 白色
     *
     * @param msg
     */
    public static void showCenterToast(String msg) {
        showCustomizeToast(Gravity.CENTER, 0, Color.parseColor("#2b2b2b"), R.drawable.bg_white_alpha, msg);
    }


    /**
     * 白色
     *
     * @param msg
     */
    public static void showBottomToast(String msg) {
        showCustomizeToast(Gravity.BOTTOM, ZxTool.dp2px(100), Color.parseColor("#2b2b2b"), R.drawable.bg_white_alpha, msg);
    }

    /**
     * 黑色
     *
     * @param msg
     */
    public static void showBottomToastLight(String msg) {
        showCustomizeToast(Gravity.BOTTOM, ZxTool.dp2px(100), Color.parseColor("#ffffff"), R.drawable.bg_black_alpha, msg);
    }
}
